﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ChoiceBinding.cs" company="">
//   
// </copyright>
// <summary>
//   The choice binding.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Bindings
{
    using Libium.UseCases;
    using Libium.Views;

    /// <summary>
    ///   The choice binding.
    /// </summary>
    public class ChoiceBinding : UseCaseBinding<IChoiceUseCase, IChoiceView>
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ChoiceBinding"/> class.
        /// </summary>
        /// <param name="service">
        /// The service. 
        /// </param>
        /// <param name="useCase">
        /// The use case. 
        /// </param>
        /// <param name="view">
        /// The view. 
        /// </param>
        public ChoiceBinding(IBindService service, IChoiceUseCase useCase, IChoiceView view)
            : base(service, useCase, view)
        {
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///   The bind.
        /// </summary>
        public override void Bind()
        {
            base.Bind();
            View.NamedValues = UseCase.NamedValues;
            View.SelectedValue = UseCase.SelectedValue;
            UseCase.SelectedValueChanged += OnElementSelectedValueChanged;
            View.SelectedValueChanged += OnViewSelectedValueChanged;
        }

        /// <summary>
        ///   The unbind.
        /// </summary>
        public override void Unbind()
        {
            base.Unbind();
            UseCase.SelectedValueChanged -= OnElementSelectedValueChanged;
            View.SelectedValueChanged -= OnViewSelectedValueChanged;
        }

        #endregion

        #region Methods

        /// <summary>
        /// The on element selected value changed.
        /// </summary>
        /// <param name="useCase">
        /// The use case. 
        /// </param>
        private void OnElementSelectedValueChanged(IChoiceUseCase useCase)
        {
            View.SelectedValue = UseCase.SelectedValue;
        }

        /// <summary>
        /// The on view selected value changed.
        /// </summary>
        /// <param name="view">
        /// The view. 
        /// </param>
        private void OnViewSelectedValueChanged(IChoiceView view)
        {
            UseCase.SelectedValue = view.SelectedValue;
        }

        #endregion
    }
}